
package BOH.GUI;

import BOH.data.EateryDBConnection;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

/**
 * The Back of House System for a
 * small restaurant.
 * 
 * Maeghan Colvin & Jennifer Havemman
 * CSC 289 0B01
 * February 13, 2014
 */

public class InventoryUpdatePanel extends JPanel
{
    // Variables
    private final JFrame frame;           
    private JPanel mainPanel;               
    private JPanel buttonPanel;             
    private JPanel inventoryPanel;          
    private JPanel neededPanel;             
    private JPanel onHandPanel;             
    private JList inventoryList;            //holds the product names
    private final JScrollPane scroll;       //causes the main panel to scroll
    private final ArrayList<String> inventoryDLM = new ArrayList<>();       
    private static ArrayList<String> results = new ArrayList<>();           
    private static ArrayList<String> neededResults = new ArrayList<>();     
    private static ArrayList<String> onHandResults = new ArrayList<>();     
    private JTextField neededTextField[];     
    private JTextField onHandTextField[];      
    private JButton updatePOHButton;
    private String sqlStatement, sqlStatement2, sqlStatement3; //SQL statements to select and update the database.

    
    /**
     * Builds the main frame, adds the main panel to a scroll pane and adds
     * a button panel and the scroll pane to the frame.
     */
    public InventoryUpdatePanel()
    {
        // Set Look and Feel.
        JFrame.setDefaultLookAndFeelDecorated(true);
        
        // Create and set up the window.
        frame = new JFrame("Inventory Count Sheet");
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        // Build and add mainPanel
        this.buildMainPanel();
        this.buildButtonPanel();
        scroll = new JScrollPane(mainPanel);
        frame.add(scroll, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.NORTH);
        
        // Add content to the window.
        frame.setSize(400, 400);
        frame.validate();
        
        // Display the window.
        frame.setVisible(true);    
    }
    /**
     * This method builds the small panel that holds an update button.
     */
    private void buildButtonPanel()
    {
        buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        updatePOHButton = new JButton("Update");
        updatePOHButton.addActionListener(new UpdateButtonActionListener());
        buttonPanel.add(updatePOHButton);
    }
    
    /**
     * This method builds the panel that holds the inventory list, product needed 
     * fields, and the product on hand field.
     */
    private void buildMainPanel()
    {
        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        
        buildInventoryPanel();
        buildNeededPanel();
        buildOnHandPanel();
        
        mainPanel.add(inventoryPanel, BorderLayout.WEST);
        mainPanel.add(neededPanel, BorderLayout.CENTER);
        mainPanel.add(onHandPanel, BorderLayout.EAST);
    }
    
    /**
     * This method builds the inventory panel that holds the list of product names.
     */
    private void buildInventoryPanel()
    {
        inventoryPanel = new JPanel();
        
        //pulls the array list and parses it into a default list model.
        results = getInventoryList();
        for (int index = 0; index < results.size(); index++)
        {
            inventoryDLM.add(index, results.get(index));
        }
        
        //initializes the list with the inventory default list model.
        inventoryList = new JList(inventoryDLM.toArray());
        inventoryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //sets cell width so the list matches the textfield size.
        inventoryList.setFixedCellHeight(50);
        inventoryList.setFixedCellWidth(100);
        inventoryList.setBorder(BorderFactory.createTitledBorder("Inventory Items"));
        inventoryPanel.add(inventoryList);
    }
    
    /**
     * Builds the panel that creates the "Product Needed" text fields. These fields
     * are parsed from the database.
     */
    private void buildNeededPanel()
    {
        neededPanel = new JPanel();
        neededPanel.setLayout(new BoxLayout(neededPanel, BoxLayout.Y_AXIS));
        neededPanel.setBorder(BorderFactory.createTitledBorder("Product Needed"));
        neededResults = getNeededList();
        neededTextField = new JTextField[neededResults.size()];
            
        // Parses the neededResults ArrayList to a series of text fields.
        for (int index = 0; index < neededResults.size(); index++)
        {
            neededTextField[index] = new JTextField();
            neededTextField[index].setEditable(false);
            neededTextField[index].setHorizontalAlignment(JTextField.RIGHT);
            neededTextField[index].setText(neededResults.get(index));
            neededPanel.add(neededTextField[index]);
        } 
    }
    
    /**
     * This method builds a panel that holds the editable "Products On Hand" 
     * text fields.
     */
    private void buildOnHandPanel()
    {
        onHandPanel = new JPanel();
        onHandPanel.setLayout(new BoxLayout(onHandPanel, BoxLayout.Y_AXIS));
        onHandPanel.setBorder(BorderFactory.createTitledBorder("Product On Hand"));
        
        // sets the size of the onHand Panel to make sure the title is readable.
        onHandPanel.setMinimumSize(new Dimension(120, 70));
        onHandPanel.setMinimumSize(new Dimension(120, 70));
        onHandPanel.setPreferredSize(new Dimension(120, 70));
        
        //retrieves an arrayList from the getOnHandList method and parses the 
        //information to a series of editable text fields. 
        onHandResults = getOnHandList();
        onHandTextField = new JTextField[onHandResults.size()];    
        for (int index = 0; index < onHandResults.size(); index++)
        {
            onHandTextField[index] = new JTextField();
            onHandTextField[index].setEditable(true);
            onHandTextField[index].setHorizontalAlignment(JTextField.RIGHT);
            onHandTextField[index].setText(onHandResults.get(index));
            // a basic key listener that stops anything from being typed in the
            // text fields aside from numbers or a period.
            onHandTextField[index].addKeyListener(new KeyAdapter() 
            {
                @Override
                public void keyTyped(KeyEvent e) 
                {
                    char character = e.getKeyChar();
                    if (((character < '0') || (character > '9' || (character == '.')))
                            && (character != '\b')) 
                    {
                        e.consume();
                    }
                }
            });
            onHandPanel.add(onHandTextField[index]);
        } 
    }
    
    //parses the product names from the database into an arrayList.
    public ArrayList<String> getInventoryList()
    {
        sqlStatement = "SELECT ProductName " +
                        "FROM Inventory " +
                        "WHERE ProductName IS NOT NULL";     
        results = EateryDBConnection.getArrayListData(sqlStatement);
       
        return results;
    }
    
    //parses the product needed amount from the database into an arrayList.
    public ArrayList<String> getNeededList()
    {
        sqlStatement2 = "SELECT ProductNeeded " +
                        "FROM Inventory";
        neededResults = EateryDBConnection.getArrayListData(sqlStatement2);
        
        return neededResults;
    }
    
    //parses the product on hand amount from the database into an arrayList.
    public ArrayList<String> getOnHandList()
    {
        sqlStatement3 = "SELECT ProductQuantityOnHand " +
                        "FROM Inventory";
        onHandResults = EateryDBConnection.getArrayListData(sqlStatement3);
        
        return onHandResults;
    }

    /**
     * updates the product on hand numbers for the inventory table based on input
     * in the onHandTextFields.
     */ 
    private class UpdateButtonActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) 
        {
            for(int index = 0; index < inventoryDLM.size(); index++)
            {
                Double newOnHand = Double.parseDouble(onHandTextField[index].getText());
                String itemName = inventoryDLM.get(index).toString();
                sqlStatement = "UPDATE Inventory " +
                                "SET ProductQuantityOnHand = " + newOnHand + " " +
                                "WHERE ProductName = '" + itemName + "' ";
                EateryDBConnection.updateDatabase(sqlStatement);
                System.out.print(sqlStatement);
            }
        }
    }
}